/**
 * 
 */
package com.pango.core.report;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory;

import com.pango.core.utils.ContextUtils;

/**
 */
public class ReportServiceImpl implements ReportService {

	private static final String REPORT_TEMPLATE_PATH = "reports/templates/";
	
	private String reportTemplatePath = REPORT_TEMPLATE_PATH;
	
	public void export(String reportId, ReportContext context) throws Exception {
		Connection conn = null;
		try {
			conn = this.getConnection();
			JasperPrint jasperPrint = fillReport(reportId, conn, context.getQueryParams());
			// 导出文件
			JRExporter exporter = buildExporter(jasperPrint, context.getOutput());
			exporter.exportReport();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != conn) {
				try {
					conn.close();
				} catch (SQLException e) {
					e = null;
				}
			}
		}
	}
	
	protected Connection getConnection() throws SQLException {
		DataSource dataSource = (DataSource)ContextUtils.getBean("dataSource");
		return dataSource.getConnection();
	}
	
	protected InputStream getTemplateInputStream(String reportId) throws Exception {
		String path = reportTemplatePath + reportId + ".jasper";
		InputStream input = getClass().getClassLoader().getResourceAsStream(path);
		if(input == null){
			throw new Exception(".jasper file not found.check configuration please. [" + path + "]");
		}
		return input;
	}
	
	/**
	 * 填充数据
	 * @param document
	 * @return
	 */
	protected JasperPrint fillReport(String reportId, Connection conn, Map<String, Object> requestParams) {
		Map<String, Object> params = new HashMap<String, Object>(requestParams);
		params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
		params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
		params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.SIMPLIFIED_CHINESE);
		params.put(JRParameter.REPORT_LOCALE, Locale.SIMPLIFIED_CHINESE);
		try {
			InputStream templateStream = getTemplateInputStream(reportId);
			return JasperFillManager.fillReport(templateStream, params, conn);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
	}
	
	/**
	 * 获得导出器
	 * @param type
	 * @return
	 * @throws IOException 
	 */
	protected JRExporter buildExporter(JasperPrint jasperPrint, OutputStream out) throws IOException {
		JRExporter exporter = new JRPdfExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
		return exporter;
	}

	/**
	 * @param reportTemplatePath the reportTemplatePath to set
	 */
	public void setReportTemplatePath(String reportTemplatePath) {
		this.reportTemplatePath = reportTemplatePath;
	}
}
